﻿using System.ComponentModel;
using System.ComponentModel.Composition;
using ScrumFactory.Composition.ViewModel;
using ScrumFactory.Composition;
using ScrumFactory.Services;
using System.Windows.Input;


namespace ScrumFactory.ProjectSummary.ViewModel
{
    
    /// <summary>
    /// Project Summary Tab View Model.
    /// </summary>
    [Export(typeof(IPanelViewModel))]
    [Export(typeof(IProjectSummaryViewModel))]
    public class ProjectSummaryViewModel : BasePanelViewModel, IPanelViewModel, IProjectSummaryViewModel, INotifyPropertyChanged {

        private bool showCreateProjectSuccessMessage;
        private Project project;

        private IEventAggregator aggregator;
        private IBackgroundExecutor executor;
        private IProjectsService projectsService;
        private IDialogService dialogService;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectSummaryViewModel"/> class.
        /// </summary>
        /// <param name="aggregator">The event aggregator.</param>
        /// <param name="burndownViewModel">The burndown view model.</param>
        /// <param name="dialogService">The dialog service.</param>
        /// <remarks>Constructor imported using MEF.</remarks>
        [ImportingConstructor()]
        public ProjectSummaryViewModel(
            [Import] IEventAggregator aggregator,
            [Import] IBackgroundExecutor executor,
            [Import] IProjectsService projectsService,            
            [Import] IDialogService dialogService,
            [Import] IBurndownViewModel burndownViewModel) {

                this.aggregator = aggregator;
                this.executor = executor;
                this.projectsService = projectsService;
                this.dialogService = dialogService;

                aggregator.Subscribe<Project>(ScrumFactoryEvent.ViewProjectDetails, OnViewProjectDetails);
                aggregator.Subscribe<Project>(ScrumFactoryEvent.ProjectCreated, OnProjectCreation);
            
                BurndownViewModel = burndownViewModel;
            
                StartProjectCommand = new DelegateCommand(StartProject);
        }


        private void StartProject() {

            System.Windows.MessageBoxResult result;

            System.DateTime startDate = Project.FirstSprint.StartDate.Date;
            if (startDate.Equals(System.DateTime.Today)) 
                result = dialogService.Show(
                    Properties.Resources.Start_project,
                    Properties.Resources.Start_project_message,
                    System.Windows.MessageBoxButton.OKCancel,
                    "/Images/Toolbar/StartProject.png");
            else
                result = dialogService.Show(
                    Properties.Resources.Start_project,
                    string.Format(Properties.Resources.Start_project_message_move_date, startDate),
                    System.Windows.MessageBoxButton.YesNoCancel,
                    "/Images/Toolbar/StartProject.png");

            if (result == System.Windows.MessageBoxResult.Cancel)
                return;

            Project.Status = (short) ProjectStatus.PROJECT_STARTED;

            executor.StartBackgroundTask(() => { projectsService.UpdateProject(Project);  }, () => { });

        }

        /// <summary>
        /// Called when the project details is viewed.
        /// </summary>
        /// <param name="project">The project.</param>
        private void OnViewProjectDetails(Project project) {
            if (justCreatedProject==null || project.ProjectUId != justCreatedProject.ProjectUId)
                ShowCreateProjectSuccessMessage = false;
            Project = project;            
            justCreatedProject = null;
        }

        private Project justCreatedProject = null;

        /// <summary>
        /// Called when a project is created.
        /// </summary>
        /// <param name="project">The created project.</param>
        private void OnProjectCreation(Project project) {
            justCreatedProject = project;
            ShowCreateProjectSuccessMessage = true;
        }


        /// <summary>
        /// Gets or sets the burndown view model.
        /// </summary>
        /// <value>The burndown view model.</value>
        public IBurndownViewModel BurndownViewModel { get; private set; }

        #region IProjectSummaryViewModel Members

        /// <summary>
        /// Gets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project {
            get {
                return project;
            }
            private set {
                project = value;
                OnPropertyChanged("Project");
            }
        }

        /// <summary>
        /// Gets the view for this View Model.
        /// </summary>
        /// <value>The view.</value>
        [Import(typeof(IProjectSummaryView))]
        public IProjectSummaryView View { get; set; }

        /// <summary>
        /// Gets whenever the success create project message should be displayied.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if show create project success message; otherwise, <c>false</c>.
        /// </value>
        public bool ShowCreateProjectSuccessMessage {
            get {
                return showCreateProjectSuccessMessage;
            }
            set {
                showCreateProjectSuccessMessage = value;
                OnPropertyChanged("ShowCreateProjectSuccessMessage");
            }
        }

        public ICommand StartProjectCommand { get; set; }

        #endregion

        #region IPanelViewModel Members


        /// <summary>
        /// Gets the panel placement.
        /// </summary>
        /// <value>The panel placement.</value>
        public PanelPlacements PanelPlacement {
            get { return PanelPlacements.ProjectTab; }
        }

        /// <summary>
        /// Gets the name of the panel.
        /// </summary>
        /// <value>The name of the panel.</value>
        public string PanelName {
            get {
                return Properties.Resources.Summary;
            }
        }

        /// <summary>
        /// Gets the panel Unique id.
        /// </summary>
        /// <value>The panel Unique id.</value>
        public string PanelUId {
            get {
                return "PROJECT_SUMMARY_TAB";
            }
        }

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        object IPanelViewModel.View {
            get {
                return this.View;
            }
        }


        /// <summary>
        /// Gets the panel display order when it is displayed at the view.
        /// </summary>
        /// <value>The panel display order.</value>
        public int PanelDisplayOrder {
            get {
                return 100;
            }
        }





        #endregion
    }
}
